// pages/search/search.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currenttab:0,
    start:0,
    currentpage:1,
    list:[],
    pagesize:20,
    cartids:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let cartids = wx.getStorageSync("cartids");
   this.setData({cartids});
  },
  navbarTap(e){
    let {currenttab} = e.target.dataset;
    this.setData({currenttab});
  },
  inputchange(e){
    let value = e.detail.value,{prop}=e.target.dataset;
    let searchinput = {};
    searchinput[prop] = value;
    this.setData(searchinput);
    
  },
  tosearch(){
    this.setData({list:[],start:0,currentpage:1});
    this.searchgoods(0);
  },
  searchgoods(start){
    wx.showLoading({
      title: '加载中',
    })
    let { keyword, pagesize } = this.data;
    if (keyword == "") {
      wx.hideLoading();
      return;
    }
    api.searchgoods({ pagesize, start, keyword }, res => {
      if (res.data.issuccess == 1) {
        let list = res.data.list, length = res.data.list.length;
        if (list.length == 0) {
          this.setData({ list: [], show: !0 });
          wx.hideLoading();
          return;
        }
        this.setData({ length, show: !1 });
        this.formatdata(list);
      }
    });
  },
  editcart(e) {
  
    let { index } = e.currentTarget.dataset, { list,cartids } = this.data,
      already = !list[index].already,
      ids = list[index].id;
    
    if (already) {
      api.addcart({ goodsid: ids }, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '添加成功',
          })
          cartids.push(ids);
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }
      });
    } else {
      api.delcart({ ids }, res => {
        if (res.data.issuccess == 1) {
          list[index].already = already;
          wx.showToast({
            title: '取消成功',
          })
          let i = cartids.indexOf(ids);
          cartids.splice(i, 1);
          this.setData({ list, cartids });
          wx.setStorageSync("cartids", cartids);
          app.badege(cartids);
        }

      })
    }
    },
  todetail(e) {
    let { id } = e.currentTarget.dataset;
    console.log(id, e);
    wx.navigateTo({
      url: '../books/detail/detail?i=' + id,
    })
  },
  formatdata(clist) {
    let {cartids} = this.data;
    clist.forEach((x, i) => {
      if (x.thumbPicPath.indexOf("http") == -1) {
        x.thumbPicPath = headimg + x.thumbPicPath;
      }
      if (cartids.indexOf(x.id) != -1) {
        clist[i].already = !0;

      }
      x.collect = 0;
      // x.detaillist = x.detaillist.split(',');
      if(x.labels){
        x.labels = x.labels.split(',');
      }
      // if (x.detaillist.length > 0) {
      //   x.detaillist.forEach((y, i) => {
      //     if (y.indexOf("http") == -1) {
      //       x.detaillist[i] = headimg + x.detaillist[i];
      //     }
      //   });
      // }
    });
    let { list } = this.data;
    list = list.concat(clist);
    this.setData({ list });
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { pagesize, currentpage, start, length } = this.data;

    if (length != pagesize) {
      wx.showToast({
        icon: "none",
        title: '再怎么扯也没有了',
      })
      return;
    }
    start = pagesize * currentpage; currentpage = currentpage + 1;

    this.setData({ start, currentpage });
    this.searchgoods(start);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})